package cn.org.timespace.easyframe.util.sqlserver;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import cn.org.timespace.easyframe.util.sqlserver.DbData;




public class ConfigManager {
	public static DbData parse(String xmlFile) throws Exception {
		DbData cb = new DbData();

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		db = dbf.newDocumentBuilder();
		Document doc = db.parse(xmlFile);
		Element root = doc.getDocumentElement();

		NodeList list = root.getElementsByTagName("use");
		Element e = (Element) list.item(0);
		String text = (((Text) e.getFirstChild()).getNodeValue()).toString();
		cb.setUse(text);

		list = root.getElementsByTagName("data-source-name");
		e = (Element) list.item(0);
		text = (((Text) e.getFirstChild()).getNodeValue()).toString();
		cb.setDataSource(text);

		list = root.getElementsByTagName("jdbc");
		Element eJdbc = (Element) list.item(0);

		e = (Element) list.item(0);
		text = (((Text) e.getFirstChild()).getNodeValue()).toString();
		cb.setJdbcDriver(text);

		e = (Element) list.item(0);
		text = (((Text) e.getFirstChild()).getNodeValue()).toString();
		cb.setUrl(text);

		e = (Element) list.item(0);
		text = (((Text) e.getFirstChild()).getNodeValue()).toString();
		cb.setUser(text);

		e = (Element) list.item(0);
		try {
			text = (((Text) e.getFirstChild()).getNodeValue()).toString();
		} catch (NullPointerException ex) {
			text = "";
		}
		cb.setPassword(text);

		list = root.getElementsByTagName("jdbc-driver");
		e = (Element) list.item(0);
		text = (((Text) e.getFirstChild()).getNodeValue()).toString();
		cb.setJdbcDriver(text);
		
		list = root.getElementsByTagName("url");
		e = (Element) list.item(0);
		text = (((Text) e.getFirstChild()).getNodeValue()).toString();
		cb.setUrl(text);
		
		list = root.getElementsByTagName("user-name");
		e = (Element) list.item(0);
		text = (((Text) e.getFirstChild()).getNodeValue()).toString();
		cb.setUser(text);
		
		list = root.getElementsByTagName("password");
		e = (Element) list.item(0);
		try{
			text = (((Text) e.getFirstChild()).getNodeValue()).toString();
		}catch(Exception ex){
			text="";
		}
		cb.setPassword(text);
		return cb;
	}

	
}
